package com.wenlong.train_ticketing_system_springboot.querymodule.producter;

import com.wenlong.train_ticketing_system_springboot.querymodule.config.RabbitConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 苏文龙
 * @version 1.0
 * @RabbitProduct 定时消息队列 消息提供者
 */
@Component
@Slf4j
public class RabbitProduct {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendDelayMessageToNotify(Integer delayTime, String content) {
        rabbitTemplate.convertAndSend(RabbitConfig.DELAY_MSG_EXCHANGE, RabbitConfig.DELAY_MSG_ROUTING_KEY, content, message -> {
            // 注意这里时间可以是 Long，而且是设置 header, delayTime (ms)
            message.getMessageProperties().setHeader("x-delay", delayTime);
            // 发送消息的时候的延迟时长
//            message.getMessageProperties().setDelay(delayTime);
            return message;
        });
//        log.info("========(ToNotify)延迟队列{}ms后执行========", delayTime);
    }

    public void sendDelayMessageToDeleteMsg(Integer delayTime, String content) {
        rabbitTemplate.convertAndSend(RabbitConfig.DELAY_MSG_DELETE_EXCHANGE, RabbitConfig.DELAY_MSG_DELETE_ROUTING_KEY, content, message -> {
            message.getMessageProperties().setHeader("x-delay", delayTime);
            return message;
        });
//        log.info("========(ToDeleteMsg)延迟队列{}ms后执行========", delayTime);
    }
}
